{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "**什么是numpy?**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "numpy是python中基于数组对象的科学计算库。\n", "提炼关键字,可以得出numpy以下三大特点:\n", "拥有n维数组对象;\n", "拥有广播功能(后面讲到);\n", "拥有各种科学计算API,任你调用;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**如何安装numpy?**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "因为numpy是一个python库,所以使用python包管理工具pip或者conda都可以安装。\n", "安装python后,打开cmd命令行,输入:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#pip install numpy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**什么是n维数组对象?**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "n维数组(ndarray)对象,是一系列同类数据的集合,可以进行索引、切片、迭代操作。\n", "numpy中可以使用array函数创建数组:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2020-09-04T05:37:45.921044Z", "start_time": "2020-09-04T05:37:45.912067Z" } }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "np.array([1,2,3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**如何区分一维、二维、多维?**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "判断一个数组是几维,主要是看它有几个轴(axis)。\n", "\n", "一个轴表示一维数组,两个轴表示二维数组,以此类推。\n", "\n", "每个轴都代表一个一维数组。\n", "\n", "比如说,二维数组第一个轴里的每个元素都是一个一维数组,也就是第二个轴。\n", "\n", "一维数组一个轴:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2020-09-04T05:38:56.810220Z", "start_time": "2020-09-04T05:38:56.807228Z" } }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1,2,3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "二维数组两个轴:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2020-09-04T05:39:21.661540Z", "start_time": "2020-09-04T05:39:21.657550Z" } }, "outputs": [ { "data": { "text/plain": [ "[[0, 1, 2], [3, 4, 5]]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[[0, 1, 2],\n", " [3, 4, 5]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "三维数组三个轴:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2020-09-04T05:39:49.201611Z", "start_time": "2020-09-04T05:39:49.196659Z" } }, "outputs": [ { "data": { "text/plain": [ "[[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[[[ 0, 1, 2],\n", " [ 3, 4, 5]],\n", "\n", " [[ 6, 7, 8],\n", " [ 9, 10, 11]]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "以此类推n维数组。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**以下表达式运行的结果分别是什么?**\n", "\n", "(提示: NaN = not a number, inf = infinity)\n", "\n", "0 * np.nan\n", "\n", "np.nan == np.nan\n", "\n", "np.inf > np.nan\n", "\n", "np.nan - np.nan\n", "\n", "0.3 == 3 * 0.1\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2020-09-06T02:29:21.779136Z", "start_time": "2020-09-06T02:29:21.774149Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nan\n", "False\n", "False\n", "nan\n", "False\n" ] } ], "source": [ "print(0 * np.nan)\n", "print(np.nan == np.nan)\n", "print(np.inf > np.nan)\n", "print(np.nan - np.nan)\n", "print(0.3 == 3 * 0.1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**将numpy的datetime64对象转换为datetime的datetime对象。**\n", "\n", "- `dt64 = np.datetime64('2020-02-25 22:10:10')`\n", "\n", "【知识点:时间日期和时间增量】\n", "- 如何将numpy的datetime64对象转换为datetime的datetime对象?" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2020-09-06T03:22:33.585786Z", "start_time": "2020-09-06T03:22:33.577808Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2020-02-25 22:10:10 \n" ] } ], "source": [ "import numpy as np\n", "import datetime\n", "\n", "dt64 = np.datetime64('2020-02-25 22:10:10')\n", "dt = dt64.astype(datetime.datetime)\n", "print(dt, type(dt))\n", "# 2020-02-25 22:10:10 " ] }, { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2020-09-06T03:23:02.362883Z", "start_time": "2020-09-06T03:23:02.357896Z" } }, "source": [ "**给定一系列不连续的日期序列。填充缺失的日期,使其成为连续的日期序列。**\n", "\n", "- `dates = np.arange('2020-02-01', '2020-02-10', 2, np.datetime64)`\n", "\n", "【知识点:时间日期和时间增量、数学函数】\n", "- 如何填写不规则系列的numpy日期中的缺失日期?" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2020-09-06T03:23:17.318967Z", "start_time": "2020-09-06T03:23:17.307994Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['2020-02-01' '2020-02-03' '2020-02-05' '2020-02-07' '2020-02-09']\n", "['2020-02-01' '2020-02-02' '2020-02-03' '2020-02-04' '2020-02-05'\n", " '2020-02-06' '2020-02-07' '2020-02-08' '2020-02-09']\n" ] } ], "source": [ "import numpy as np\n", "\n", "dates = np.arange('2020-02-01', '2020-02-10', 2, np.datetime64)\n", "print(dates)\n", "# ['2020-02-01' '2020-02-03' '2020-02-05' '2020-02-07' '2020-02-09']\n", "\n", "out = []\n", "for date, d in zip(dates, np.diff(dates)):\n", " out.extend(np.arange(date, date + d))\n", "fillin = np.array(out)\n", "output = np.hstack([fillin, dates[-1]])\n", "print(output)\n", "# ['2020-02-01' '2020-02-02' '2020-02-03' '2020-02-04' '2020-02-05'\n", "# '2020-02-06' '2020-02-07' '2020-02-08' '2020-02-09']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**如何得到昨天,今天,明天的的日期**\n", "\n", "【知识点:时间日期】\n", "- (提示: np.datetime64, np.timedelta64)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2020-09-14T09:21:51.819371Z", "start_time": "2020-09-14T09:21:51.815870Z" } }, "outputs": [], "source": [ "yesterday = np.datetime64('today', 'D') - np.timedelta64(1, 'D')\n", "today = np.datetime64('today', 'D')\n", "tomorrow = np.datetime64('today', 'D') + np.timedelta64(1, 'D')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2020-09-14T09:21:53.263787Z", "start_time": "2020-09-14T09:21:53.258801Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Yesterday is 2020-09-13\n", "Today is 2020-09-14\n", "Tomorrow is 2020-09-15\n" ] } ], "source": [ "print (\"Yesterday is \" + str(yesterday))\n", "print (\"Today is \" + str(today))\n", "print (\"Tomorrow is \"+ str(tomorrow))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**创建从0到9的一维数字数组。**\n", "\n", "【知识点:数组的创建】\n", "- 如何创建一维数组?\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2020-09-04T05:21:17.599471Z", "start_time": "2020-09-04T05:21:17.595447Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 1 2 3 4 5 6 7 8 9]\n" ] } ], "source": [ "#【答案】\n", "\n", "import numpy as np\n", "\n", "arr = np.arange(10)\n", "print(arr)\n", "# [0 1 2 3 4 5 6 7 8 9]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**创建一个元素全为`True`的 3×3 数组。**\n", "\n", "【知识点:数组的创建】\n", "- 如何创建一个布尔数组?" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2020-09-04T05:23:28.582218Z", "start_time": "2020-09-04T05:23:28.578229Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ True True True]\n", " [ True True True]\n", " [ True True True]]\n" ] } ], "source": [ "#答案\n", "import numpy as np\n", "\n", "arr = np.full([3, 3], True, dtype=np.bool)\n", "print(arr)\n", "# [[ True True True]\n", "# [ True True True]\n", "# [ True True True]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**创建一个长度为10并且除了第五个值为1的空向量**\n", "\n", "【知识点:数组的创建】\n", "\n", "- (提示: array[4])" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2020-09-04T05:57:59.004744Z", "start_time": "2020-09-04T05:57:58.999757Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]\n" ] } ], "source": [ "Z = np.zeros(10)\n", "Z[4] = 1\n", "print(Z)" ] }, { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2020-09-04T05:58:20.995843Z", "start_time": "2020-09-04T05:58:20.990855Z" } }, "source": [ "**创建一个值域范围从10到49的向量**\n", "\n", "【知识点:创建数组】\n", "\n", "- (提示: np.arange)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2020-09-04T05:58:45.316583Z", "start_time": "2020-09-04T05:58:45.312592Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33\n", " 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]\n" ] } ], "source": [ "Z = np.arange(10,50)\n", "print(Z)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**创建一个 3x3x3的随机数组**\n", "\n", "【知识点:创建数组】\n", "\n", "(提示: np.random.random)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2020-09-06T02:25:01.890974Z", "start_time": "2020-09-06T02:25:01.555976Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[[0.9656547 0.98794518 0.97311304]\n", " [0.70795913 0.48301228 0.99415693]\n", " [0.88059252 0.36213986 0.09431051]]\n", "\n", " [[0.19983998 0.27391144 0.75292906]\n", " [0.19717369 0.97399681 0.84519249]\n", " [0.1990827 0.75098517 0.49597504]]\n", "\n", " [[0.59227295 0.13609747 0.278576 ]\n", " [0.0652865 0.30666851 0.44145043]\n", " [0.28980157 0.64063686 0.13204375]]]\n" ] } ], "source": [ "Z = np.random.random((3,3,3))\n", "print(Z)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**创建一个二维数组,其中边界值为1,其余值为0**\n", "\n", "【知识点:二维数组的创建】\n", "\n", "- (提示: array[1:-1, 1:-1])" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2020-09-06T02:26:25.019587Z", "start_time": "2020-09-06T02:26:25.015563Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n", " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]\n" ] } ], "source": [ "Z = np.ones((10,10))\n", "Z[1:-1,1:-1] = 0\n", "print(Z)" ] }, { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2020-09-06T03:24:54.695345Z", "start_time": "2020-09-06T03:24:54.691389Z" } }, "source": [ "**创建长度为10的numpy数组,从5开始,在连续的数字之间的步长为3。**\n", "\n", "【知识点:数组的创建与属性】\n", "- 如何在给定起始点、长度和步骤的情况下创建一个numpy数组序列?" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2020-09-06T03:25:11.711285Z", "start_time": "2020-09-06T03:25:11.708316Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 5 8 11 14 17 20 23 26 29 32]\n" ] } ], "source": [ "import numpy as np\n", "\n", "start = 5\n", "step = 3\n", "length = 10\n", "a = np.arange(start, start + step * length, step)\n", "print(a) # [ 5 8 11 14 17 20 23 26 29 32]" ] }, { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2020-09-06T03:25:34.766852Z", "start_time": "2020-09-06T03:25:34.762862Z" } }, "source": [ "**将本地图像导入并将其转换为numpy数组。**\n", "\n", "【知识点:数组的创建与属性】\n", "- 如何将图像转换为numpy数组?" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "ExecuteTime": { "end_time": "2020-09-06T03:27:06.728838Z", "start_time": "2020-09-06T03:27:06.496621Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(959, 959, 3) uint8\n" ] } ], "source": [ "import numpy as np\n", "from PIL import Image\n", "\n", "img1 = Image.open('test.jpg')\n", "a = np.array(img1)\n", "\n", "print(a.shape, a.dtype)\n", "# (959, 959, 3) uint8" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.10" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }